opentelemetry-sdk: drop NaN measurements at instrument level to prevent aggregation poisoning#5336
Open
RudraDudhat2509 wants to merge 4 commits into
Open
Conversation
Author
herin049
reviewed
Jun 19, 2026
xrmx
requested changes
Jun 22, 2026
| for callback in self._callbacks: | ||
| try: | ||
| for api_measurement in callback(callback_options): | ||
| if math.isnan(api_measurement.value): |
Contributor
There was a problem hiding this comment.
This does not catch math.inf though which I guess will have the same issue as math.nan, what about using math.isfinite to cover both instead?
Author
There was a problem hiding this comment.
Good catch sir, completely missed the inf check, apologies, will update soon
Author
|
hey sir, switched all isnan checks to math.isfinite to cover both NaN and Inf, and added inf test cases for each instrument type |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
NaNmeasurements passed to metric instruments (e.g.counter.add(float("nan"))) were silently folded into running aggregations, permanently poisoning the sum, all subsequent exports emittednanwith no recovery pathif amount < 0guard onCounterandHistogramis bypassed byNaNbecause all IEEE 754 comparisons againstNaNreturnFalsemath.isnan()check at the instrument level (same layer as the existing negative-value guard) for all sync instruments (Counter,UpDownCounter,Histogram,Gauge) and in the async callback path, matching the approach taken in the Java SDK (open-telemetry/opentelemetry-java#5846, fixed in PR #5859)Closes #5330
What was tested
test_add_nantoTestCounterandTestUpDownCountertest_record_nantoTestHistogramtest_set_nantoTestGaugetest_nan_callback_value_is_droppedtoTestObservableGauge(verifies NaN from async callbacks is skipped, valid measurements still pass through)python -m pytest opentelemetry-sdk/tests/metrics/test_instrument.py)counter.add(1) → add(NaN) → add(5)now yieldssum = 6, notnan